<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *知识
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'知识列表'),
	array('op'=>'edit','title'=>'添加知识'),	
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'video'=>'');
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('jy_kexie_user',$data);
		$params['mid']=$mid['mid'];
		$return['params']['mid']=$mid['mid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_kexie_video',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$user=pdo_get('jy_kexie_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$member=pdo_get('core_members',array('id'=>$item['mid']),array('credit1','credit2','mobile','avatar','nickname','username','sex'));
			$item['member']=$member;
			$item['user']=$user;
			$item['likenum']=pdo_count('jy_kexie_video_log',array('video_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'islike'=>1),'id');
			$item['paynum']=pdo_count('jy_kexie_video_log',array('video_id'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1),'id');
			$item['url']=custom_url('jy_kexie/video_detail//id='.$item['id'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'total'=>$total,'psize'=>$psize,'list'=>array(
			array('name'=>'id','title'=>'知识ID','type'=>'string'),
			array('name'=>'title','title'=>'知识名称','type'=>'string'),
			array('name'=>'group_id','title'=>'知识分类','type'=>'select','options'=>'groups'),
			array('type'=>'member','title'=>'发布人','name'=>'member'),
			array('name'=>'url','title'=>'知识链接','type'=>'copy'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'likenum','title'=>'点赞量','type'=>'string'),
			array('name'=>'paynum','title'=>'点赞量','type'=>'string'),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'updatetime','title'=>'刷新时间','type'=>'time'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		),'action' => 
		    array (
			    array('type'=>'post','op'=>'ok','show'=>array('status'=>array(0,2)),'title'=>'审核通过','message'=>'确定通过该知识吗！'),
			    array('type'=>'post','op'=>'no','show'=>array('status'=>array(0,1)),'title'=>'不通过','message'=>'确定忽略该知识吗！'),
				array('type'=>'post','op'=>'up','title'=>'刷新','message'=>'确定刷新该文章吗！'),
		)),
		'search'=>array(
			array('name'=>'id','width'=>15,'title'=>'知识ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'发布人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'知识标题','type'=>'string'),
		)
	);
	$return['groups']=pdo_getall('jy_kexie_video_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_kexie_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3);
	}
	$item['videos']=$item['videos']?json_decode($item['videos'],true):array();
	$item['commissions']=json_decode($item['commissions'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回知识列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'知识名称','type'=>'string'),
					array('name'=>'thumb','title'=>'知识封面','type'=>'image','help'=>'留空会自动截取知识第一张图片/图集第一张'),
					array('type'=>'choose','title'=>'选择发布人','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
					array('name'=>'group_id','title'=>'分类','type'=>'select','options'=>'groups'),
					array('name'=>'worker_id','title'=>'关联老师','type'=>'select','options'=>'workers','option_title'=>'name'),
					array('name'=>'desc','title'=>'知识介绍','type'=>'text'),
					array('name'=>'type','title'=>'知识类型','type'=>'radio','options'=>'types'),
					array('name'=>'price','title'=>'知识价格','type'=>'string','show'=>array('type'=>1)),
					array('type'=>'richtext','title'=>'知识内容','name'=>'content' , 'help'=>''),
					array('name'=>'readnum','title'=>'阅读人数','type'=>'string'),
					array('name'=>'times','title'=>'用时','type'=>'unitstring','unit'=>'小时'),
					array('name'=>'times_title','title'=>'学习时间自定义','type'=>'string','help'=>'默认显示：学习时间'),
					array('name'=>'star','title'=>'难易星数','type'=>'radio','options'=>'star'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$xuni_types=$this->gift_type;
	foreach($xuni_types as $k=>$xv){
		$htmls['edit'][]=array('type'=>'switch','name'=>'commissions.credit'.$k.'_diclose','title'=>$xv.'是否关闭抵扣');
		$htmls['edit'][]=array('type'=>'unitstring','unit'=>'%','title'=>$xv.'最大抵现比例','name'=>'commissions.credit'.$k.'_dirate','help'=>'默认100%抵扣，如果填写50，表示最大抵扣订单额度的50%');
	}
	$return['types']=$this->video_types;
	$return['star']=array(1=>'1星',2=>'2星',3=>'3星',4=>'4星',5=>'5星');
	$return['groups']=pdo_getall('jy_kexie_video_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=pdo_getall('jy_kexie_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		exi('请选择发布人！','error');
	}
	if(!$postdata['title']){
		exi('请填写知识名称！','error');
	}
	if(!$postdata['group_id']){
		exi('请填写知识分类！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'thumb'=>trim($postdata['thumb']),
		'type'=>intval($postdata['type']),
		'mid'=>intval($postdata['mid']),
		'desc'=>trim($postdata['desc']),
		'video'=>$postdata['videos'][0]?$postdata['videos'][0]['video']:'',
		'videos'=>ijson_encode($postdata['videos']),
		'content'=>trim($postdata['content']),
		'readnum'=>intval($postdata['readnum']),
		'group_id'=>intval($postdata['group_id']),
		'ewm'=>trim($postdata['ewm']),
		'ewmurl'=>trim($postdata['ewmurl']),
		'status'=>intval($postdata['status']),
		'price'=>trim($postdata['price']),
		'star'=>intval($postdata['star']),
		'times'=>intval($postdata['times']),
		'times_title'=>trim($postdata['times_title']),
		'isfen'=>intval($postdata['isfen']),
		'commissions'=>ijson_encode($postdata['commissions']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kexie_video',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_kexie_video',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择知识！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_kexie_video',array('status'=>intval($post['status']),'updatetime'=>TIMESTAMP),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='no'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要忽略的知识！','error');
	}
	pdo_update('jy_kexie_video',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已忽略该知识！');
}
if($_RQ['op']=='ok'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要审核的知识！','error');
	}
	pdo_update('jy_kexie_video',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已审核通过该知识！');
}
if($_RQ['op']=='up'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要刷新的文章！','error');
	}
	pdo_update('jy_kexie_video',array('updatetime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已刷新该文章！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kexie_video',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}